local xuanfu = fk.CreateSkill {
  name = "xiaobai__xuanfu"
}

Fk:loadTranslationTable {
  ["xiaobai__xuanfu"] = "喧富",
  [":xiaobai__xuanfu"] = "出牌阶段限一次，你可以与一名其他角色拼点，拼点牌牌名在本局使用的牌堆中更少者视为再赢一次；"
      .. "花色在场上更少者视为再赢一次，赢的更多者获得场上至多其赢的次数张牌，更少者可对对方使用其没赢的次数张【杀】。",
  ["#Xiaobai__xuanfu1"] = "%from 在 %from 和 %to 的拼点中多赢了一次",
  ["#Xiaobai__xuanfu2"] = "%from 在 %to 和 %from 的拼点中多赢了一次",
  ["#Xiaobai__xuanfu3"] = "%from 在 %from 和 %to 的拼点中赢了%arg次",
  ["#Xiaobai__xuanfu4"] = "%to 在 %from 和 %to 的拼点中赢了%arg次",
  ["#xiaobai__xuanfu_chooseToGet"] = "喧富：你可以选择一名角色获得其场上一张牌",
  ["#xiaobai__xuanfu_getGround"] = "喧富：你可以获得 %dest 场上的一张牌",
  ["#xiaobai__xuanfu_useSlash"] = "喧富：你可以对 %dest 使用一张【杀】"
}

xuanfu:addEffect(fk.GameStart, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self) and player == target
  end,
  on_refresh = function(self, event, target, player, data)
    for _, card in ipairs(Fk:getAllCardIds()) do
      player.room:addPlayerMark(player, "xiaobai__xuanfu_" .. Fk:getCardById(card).trueName, 1)
      
    end
  end
})

xuanfu:addEffect("active", {
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards, card, extra_data)
    return #selected == 0 and to_select ~= player and to_select:getHandcardNum() > 0
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(xuanfu.name) < 1
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local pindian = player:pindian({ target }, xuanfu.name)
    local win_times = { [player.id] = 0, [target.id] = 0 }
    local winner = pindian.results[target].winner
    if winner ~= nil then
      win_times[winner.id] = win_times[winner.id] + 1
    end
    local cp = pindian.fromCard
    local ct = pindian.results[target].toCard
    if cp ~= nil and ct ~= nil then
      
      local suits = { [Card.Spade] = 0, [Card.Club] = 0, [Card.Diamond] = 0, [Card.Heart] = 0 }
      for _, p in ipairs(room.players) do
        table.forEach(p:getCardIds("ej"),
          function(id) suits[Fk:getCardById(id).suit] = suits[Fk:getCardById(id).suit] + 1;print(Fk:getCardById(id).suit) end)
      end
      if suits[cp.suit] < suits[ct.suit] then
        win_times[player.id] = win_times[player.id] + 1
        room:sendLog{
          type = "#Xiaobai__xuanfu1",
          from = player.id,
          to = {target.id},
          toast = true
        }
        room:delay(400)
      elseif suits[cp.suit] ~= suits[ct.suit] then
        win_times[target.id] = win_times[target.id] + 1
        room:sendLog{
          type = "#Xiaobai__xuanfu2",
          to = {player.id}, 
          player = target.id,
          toast = true,
        }
        room:delay(400)
      end
      if player:getMark("xiaobai__xuanfu_" .. cp.trueName) < player:getMark("xiaobai__xuanfu_" .. ct.trueName) then
        win_times[player.id] = win_times[player.id] + 1
        room:sendLog{
          type = "#Xiaobai__xuanfu1",
          from = player.id,
          to = {target.id},
          toast = true
        }
      elseif player:getMark("xiaobai__xuanfu_" .. cp.trueName) ~= player:getMark("xiaobai__xuanfu_" .. ct.trueName) then
        win_times[target.id] = win_times[target.id] + 1
        room:sendLog{
          type = "#Xiaobai__xuanfu2",
          to = {player.id}, 
          player = target.id,
          toast = true,
        }
      end
    end
    room:sendLog{
      type = "#Xiaobai__xuanfu3",
      from = player.id,
      to = {target.id},
      arg = win_times[player.id]
    }
    room:delay(200)
    room:sendLog{
      type = "#Xiaobai__xuanfu4",
      from = player.id,
      to = {target.id},
      arg = win_times[target.id]
    }
    if win_times[player.id] == win_times[target.id] then
      return 
    end
    local true_winner = player
    if win_times[player.id] < win_times[target.id] then
      true_winner = target
    end
    if true_winner then
      for i = 1, win_times[true_winner.id] do
        local targets = table.filter(room.alive_players, function(p) return #p:getCardIds("ej") > 0 end)
        if #targets > 0 then
          local to = room:askToChoosePlayers(true_winner, {
            prompt = "#xiaobai__xuanfu_chooseToGet",
            targets = targets,
            max_num = 1,
            min_num = 1,
            skill_name = xuanfu.name,
          })
          if #to == 0 then break end
          local cards = room:askToChooseCard(true_winner, {
            prompt = "#xiaobai__xuanfu_getGround::"..to[1].id,
            target = to[1],
            flag = "ej",
            skill_name = xuanfu.name,
          })
          room:obtainCard(true_winner, cards, false, fk.ReasonPrey, player, xuanfu.name)
        else
          break
        end
      end
      local loser = player
      if player == true_winner then loser = target end
      for i = 1, 3 - win_times[loser.id] do
        local use = room:askToUseCard(loser, {
          pattern = "slash",
          prompt = "#xiaobai__xuanfu_useSlash::"..true_winner.id,
          skill_name = xuanfu.name,
          extra_data = {
            must_targets = {true_winner.id} 
          }, 
        }) 
        if not use then
          break
        end
        room:useCard(use)
      end
    end
  end
})

return xuanfu
